"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 数据库备机控制文件权限丢失，数据库主机异常重启。
Create At   : 2020/07/20
@zou_jialiang0505328114
Description :
    步骤 1.启动主备数据库，判断数据库状态；
    步骤 2.修改备机数据库控制文件权限；
    步骤 3.异常停止主库
    步骤 4.主机数据库重启
Expect      :    
History     :   
    modified:2021/02/26 by @wan005:pg_ctl文件权限为600,且修改权限后不判断数据库状态，恢复后正常即可
"""
import os
import unittest
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case145.py start------------')
        self.rootNode = Node()
        self.com = Common()
        self.constant = Constant()
        self.db_primary_user = Node(node='PrimaryDbUser')
        self.db_primary_root = Node(node='PrimaryRoot')
        self.db_standby1_root = Node(node='Standby1Root')
        self.db_standby1_user = Node(node='Standby1DbUser')
        self.t_name = f'test{str(int(time.time()))}'
        self.dir_path = os.path.join(macro.DB_INSTANCE_PATH, 'global', 'pg_control')

    def test_startdb(self):
        self.log.info('----------修改备机数据库控制文件权限---------------')
        chmod_cmd = 'chmod 000 ' + self.dir_path
        self.log.info(chmod_cmd)
        msg = self.db_standby1_root.sh(chmod_cmd).result()
        self.log.info(msg)
        self.assertEqual(msg, '')

        self.log.info('----------新建数据表后，查看数据库状态---------------')
        msg = self.com.createLargeNumTable(
            self.db_primary_user, self.t_name, 1, 1)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg)

        time.sleep(2)

        status = self.commonshpri.getDbClusterStatus('detail')
        self.log.info(status)
        self.assertIn(self.constant.P_NORMAL, status)

        self.log.info('----------异常停止主库-----------------')
        msg1 = self.com.kill_pid(self.db_primary_root, 9)
        self.assertEqual(msg1, '')
        status = self.commonshpri.getDbClusterStatus('detail')
        self.log.info(status)
        self.assertIn(self.constant.STOP_STATUS_MSG, status)

        self.log.info('----------主机数据库重启-----------------')
        msg1 = self.commonshpri.startDbCluster()
        self.log.info(f"result is {msg1}, 文件权限异常，启动不做断言")

        self.log.info('----------还原权限-------------------')
        chmod_cmd = 'chmod 600 ' + self.dir_path
        self.log.info(chmod_cmd)
        result = self.db_standby1_root.sh(chmod_cmd).result()
        self.log.info(result)

        self.log.info('-----------重启集群-----------')
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

    def tearDown(self):
        self.log.info('----------this is tearDown-----------')
        # 还原备机数据库控制文件权限
        chmod_cmd = 'chmod 600 ' + self.dir_path
        self.log.info(chmod_cmd)
        msg = self.db_standby1_root.sh(chmod_cmd).result()
        self.log.info(msg)

        # 主机数据库重启
        msg1 = self.commonshpri.startDbCluster()
        self.log.info(msg1)

        status = self.commonshpri.getDbClusterStatus('detail')
        self.log.info(status)

        # 清理环境，删除表
        msg = self.com.createLargeNumTable(self.db_primary_user, self.t_name, del_sign=True)
        self.log.info(msg)
        self.log.info('-------------reliability_dbsys145.py finish-----------------')